package com.keyinong.caizhengting.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.activity.FanqiedingcanActivity;
import com.keyinong.caizhengting.entity.GouwucheEntity;

import java.util.List;

/**
 * 佛祖保佑 永无BUG
 *
 * @author WangZhiYao
 * @date 2018/4/7
 */
public class ShoppingCartsAdapter extends BaseAdapter {

    private Context mContext;
    private List<GouwucheEntity> mCartsList;
    private int mType;

    public ShoppingCartsAdapter(Context context, List<GouwucheEntity> carsList, int type) {
        this.mContext = context;
        this.mCartsList = carsList;
        this.mType = type;
    }

    @Override
    public int getCount() {
        return mCartsList.size();
    }

    @Override
    public Object getItem(int i) {
        return mCartsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (null == view) {
            holder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_gouwuche, null);

            holder.mTvGoodsName = (TextView) view.findViewById(R.id.tv_shangpinming2);
            holder.mTvContent = (TextView) view.findViewById(R.id.tv_content2);
            holder.mTvPrice = (TextView) view.findViewById(R.id.tv_price2);
            holder.mIbAdd = (ImageButton) view.findViewById(R.id.ib_add2);
            holder.mIbMinus = (ImageButton) view.findViewById(R.id.ib_less2);
            holder.mIbDelete = (ImageButton) view.findViewById(R.id.ib_delete);
            holder.mEtAmount = (EditText) view.findViewById(R.id.et_amount2);
            holder.mTvLimit = (TextView) view.findViewById(R.id.tv_lim);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if ("1".equals(mCartsList.get(i).getShelve())) {
            holder.mTvLimit.setVisibility(View.VISIBLE);
            holder.mTvLimit.setText("商品已下架");
            setMsg("shelve", "1", i);
        } else {
            holder.mTvLimit.setVisibility(View.GONE);
        }

        holder.mTvGoodsName.setText(mCartsList.get(i).getName());
        holder.mTvContent.setText(mCartsList.get(i).getContent());
        holder.mTvPrice.setText(mCartsList.get(i).getPrice());
        holder.mEtAmount.setText(mCartsList.get(i).getAmount());

        return view;
    }

    private void setMsg(String sopt, String str, int index) {
        Message message = Message.obtain();
        Bundle bundle = new Bundle();
        bundle.putString("sopt1", sopt);
        bundle.putString("str1", str);
        bundle.putInt("index1", index);
        message.obj = bundle;
        FanqiedingcanActivity.myhanler.sendMessage(message);
    }

    class ViewHolder {
        /**
         * 商品名
         */
        TextView mTvGoodsName;
        /**
         * 说明
         */
        TextView mTvContent;
        /**
         * 价格
         */
        TextView mTvPrice;
        /**
         * 加
         */
        ImageButton mIbAdd;
        /**
         * 减
         */
        ImageButton mIbMinus;
        /**
         * 删除
         */
        ImageButton mIbDelete;
        /**
         * 数量
         */
        EditText mEtAmount;
        /**
         * 库存
         */
        TextView mTvLimit;
    }
}
